#coding=utf-8
#Boa:Frame:FrameReportPurchase

import wx

from MyListCtrl import *
from product import *
from ctrlAider import *
from wx.lib.splitter import MultiSplitterWindow

import PanelSkuSearch

def create(parent):
    return FrameReportPurchase(parent)

[wxID_FRAMEREPORTPURCHASE, wxID_FRAMEREPORTPURCHASECHECKBOX_LESS30, 
 wxID_FRAMEREPORTPURCHASECHECKBOX_ONLY_WARNING, 
 wxID_FRAMEREPORTPURCHASECHECKBOX_SHOW_DISABLE, 
] = [wx.NewId() for _init_ctrls in range(4)]

class FrameReportPurchase(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAMEREPORTPURCHASE,
              name=u'FrameReportPurchase', parent=prnt, pos=wx.Point(607, 0),
              size=wx.Size(1226, 698), style=wx.DEFAULT_FRAME_STYLE,
              title=u'\u5e93\u5b58\u91c7\u8d2d')
        self.SetClientSize(wx.Size(1210, 660))
        self.SetBackgroundStyle(wx.BG_STYLE_SYSTEM)
        self.Center(wx.HORIZONTAL)
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetAutoLayout(False)
        self.SetBackgroundColour(wx.Colour(183, 209, 173))
        self.Bind(wx.EVT_CLOSE, self.OnFrameReportPurchaseClose)

        self.checkBox_only_warning = wx.CheckBox(id=wxID_FRAMEREPORTPURCHASECHECKBOX_ONLY_WARNING,
              label=u'\u53ea\u663e\u793a\u5e93\u5b58\u9884\u8b66\u5546\u54c1',
              name=u'checkBox_only_warning', parent=self, pos=wx.Point(160, 10),
              size=wx.Size(240, 16), style=0)
        self.checkBox_only_warning.SetValue(True)
        self.checkBox_only_warning.Bind(wx.EVT_CHECKBOX,
              self.OnCheckBox_only_warningCheckbox,
              id=wxID_FRAMEREPORTPURCHASECHECKBOX_ONLY_WARNING)

        self.checkBox_show_disable = wx.CheckBox(id=wxID_FRAMEREPORTPURCHASECHECKBOX_SHOW_DISABLE,
              label=u'\u663e\u793a\u7981\u7528\u5546\u54c1',
              name=u'checkBox_show_disable', parent=self, pos=wx.Point(936, 10),
              size=wx.Size(264, 16), style=0)
        self.checkBox_show_disable.SetValue(False)
        self.checkBox_show_disable.Bind(wx.EVT_CHECKBOX,
              self.OnCheckBox_show_disableCheckbox,
              id=wxID_FRAMEREPORTPURCHASECHECKBOX_SHOW_DISABLE)

        self.checkBox_less30 = wx.CheckBox(id=wxID_FRAMEREPORTPURCHASECHECKBOX_LESS30,
              label=u'\u53ea\u663e\u793a\u5e93\u5b58\u5c11\u4e8e30\u5929\u9500\u91cf\u7684\u5546\u54c1',
              name=u'checkBox_less30', parent=self, pos=wx.Point(488, 8),
              size=wx.Size(272, 16), style=0)
        self.checkBox_less30.SetValue(False)
        self.checkBox_less30.Bind(wx.EVT_CHECKBOX,
              self.OnCheckBox_less30Checkbox,
              id=wxID_FRAMEREPORTPURCHASECHECKBOX_LESS30)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        # left:category  right:product
        self.splitterWin = MultiSplitterWindow(self, style=wx.SP_LIVE_UPDATE)
        # up:product down:purchase
        self.splitterRight = MultiSplitterWindow(self.splitterWin, style=wx.SP_LIVE_UPDATE)
        # left:planed mid:ordered right:shipped
        self.splitterPurchase = MultiSplitterWindow(self.splitterRight, style=wx.SP_LIVE_UPDATE)
        
        self.categoryList = MyCategoryTreeCtrl(self.splitterWin)

        #self.categoryList.SetSize(wx.Size(140, 580))
        #self.categoryList.Move(wx.Point(10, 10))

        self.m_sku=((),())
        self.panelSku = PanelSkuSearch.PanelSkuSearch(self.splitterRight, self)
        self.panelSku.set_category(1) # 电焊网

        self.proList = MyListCtrlProductPurchase(self.splitterRight)
        #self.proList.SetSize(wx.Size(1040, 300))
        #self.proList.Move(wx.Point(160, 40))

        #self.lcPurDetail = MyListCtrlProPurDetail(self)
        #self.lcPurDetail.SetSize(wx.Size(510, 230))
        #self.lcPurDetail.Move(wx.Point(160, 350))
            
        #self.lcSaleDetail = MyListCtrlProSaleDetail(self)
        #self.lcSaleDetail.SetSize(wx.Size(510, 230))
        #self.lcSaleDetail.Move(wx.Point(680, 350))
        
        self.lcPurDetail = MyListCtrlProPurDetail(self.splitterPurchase)
        # self.lcPurchasePlan = MyListCtrlPurchasePlan(self.splitterPurchase
        #     , MyListCtrlPurchasePlan.TYPE_PLANED)
        # self.lcPurchaseOrder = MyListCtrlPurchasePlan(self.splitterPurchase
        #     , MyListCtrlPurchasePlan.TYPE_ORDERED)
        # self.lcPurchaseShipped = MyListCtrlPurchasePlan(self.splitterPurchase
        #     , MyListCtrlPurchasePlan.TYPE_SHIPPED)

        self.lcSaleMonth = MyListCtrlProductMonthReport(self.splitterPurchase)
        self.lcSaleRegion = MyListCtrlProductRegionReport(self.splitterPurchase)
        
        self.splitterPurchase.AppendWindow(self.lcPurDetail, 530)
        self.splitterPurchase.AppendWindow(self.lcSaleMonth, 250)
        self.splitterPurchase.AppendWindow(self.lcSaleRegion, 250)
        
        #self.splitterPurchase.AppendWindow(self.lcPurchasePlan, 20)
        #self.splitterPurchase.AppendWindow(self.lcPurchaseOrder, 20)
        #self.splitterPurchase.AppendWindow(self.lcPurchaseShipped, 20)
        self.splitterPurchase.SetOrientation(wx.HORIZONTAL)
        self.splitterPurchase.SizeWindows()
        
        #self.splitterRight.SplitHorizontally(self.proList, self.splitterPurchase, 240)
        self.splitterRight.AppendWindow(self.panelSku, 100)
        self.splitterRight.AppendWindow(self.proList, 350)
        self.splitterRight.AppendWindow(self.splitterPurchase, 100)
        self.splitterRight.SetOrientation(wx.VERTICAL)
        self.splitterRight.SizeWindows()
        
        self.splitterWin.AppendWindow(self.categoryList, 150)
        self.splitterWin.AppendWindow(self.splitterRight, 250)
        
        self.splitterWin.Move(wx.Point(10, 30))
        # 设置大小为什么必须放在最后？放在AppendWindow之前导致sashPos参数不起作用
        self.splitterWin.SetSize(wx.Size(1190, 620))
        self.splitterWin.SetOrientation(wx.HORIZONTAL)
        self.splitterWin.SizeWindows()
        
                
        #self.splitterWin.SplitVertically(self.categoryList, self.splitterRight, 140)
        
        self.categoryList.Bind(wx.EVT_TREE_SEL_CHANGED,
                                self.OnTreeSelChanged)
        self.proList.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnProAllItemSelected)
        #self.proList.Bind(wx.EVT_LEFT_DCLICK, self.OnProListLeftDclick)
        
        #self.lcPurchasePlan.Bind(wx.EVT_LEFT_DCLICK, self.OnPlanedListLeftDclick)
        #self.lcPurchaseOrder.Bind(wx.EVT_LEFT_DCLICK, self.OnOrderedListLeftDclick)
        #self.lcPurchaseShipped.Bind(wx.EVT_LEFT_DCLICK, self.OnShippedListLeftDclick)
        
        self.curKeywords = []
        self.curCategory = None
        #self.refreshProductList() # 没必要启动时刷新

    def OnFrameReportPurchaseClose(self, event):
        self.Show(False)
        
    def OnTreeSelChanged(self, event):
        self.curCategory = self.categoryList.getSelection()
        self.refreshProductList()

        self.m_sku=((),())
        self.panelSku.set_category(self.curCategory.getKeyVal())
        event.Skip()
        
    def OnProAllItemSelected(self, event):

        item = self.proList.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        proId = int(self.proList.GetItem(item, self.proList.iId).GetText())
        self.lcPurDetail.refresh({'proId':proId})
        #self.lcSaleDetail.refresh({'proId':proId})
        self.lcSaleMonth.refresh({'proId':proId})
        self.lcSaleRegion.refresh({'proId':proId})

    def refreshProductList(self):

        categoryIds = []
        if (self.curCategory is not None):
            categoryId = self.curCategory.getFieldVal(index=CDataCategory.COL_ID)
            categoryIds = CDataCategory.getChildId(categoryId)
        
        keywordIds = []

        self.proList.setFilter(keywordIds, categoryIds, self.m_sku)
        
        self.proList.refresh()
        
        # self.lcPurchasePlan.setFilter(categoryIds)
        # self.lcPurchaseOrder.setFilter(categoryIds)
        # self.lcPurchaseShipped.setFilter(categoryIds)
        
        # self.lcPurchasePlan.refresh()
        # self.lcPurchaseOrder.refresh()
        # self.lcPurchaseShipped.refresh()

    def OnCheckBox_only_warningCheckbox(self, event):
        self.checkBox_less30.SetValue(False)
        self.proList.setOnlyWarn(self.checkBox_only_warning.GetValue())
        self.proList.refresh()
        event.Skip()

    def OnCheckBox_show_disableCheckbox(self, event):
        self.proList.setShowUnused(self.checkBox_show_disable.GetValue())
        self.proList.refresh()
        event.Skip()
        
    def OnProListLeftDclick(self, event):
        '''左键双击产品列表,弹出产品数量、价格窗口'''
        import DlgProGX

        item = self.proList.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            return
            
        proId,proName,proSpec,unit,price = self.proList.getProSaleInfo(item)
        dlg = DlgProGX.create(self)
        dlg.setData(proId=proId, unit=unit, price=price)
        dlg.setInbound(True)
        
        if (dlg.ShowModal() == wx.ID_OK):
            CDataProduct.add_purchase_plan(proId,dlg.unit,dlg.cnt,dlg.price)
            self.lcPurchasePlan.refresh()
    
    def RefreshPurchasePlan(self):
        # self.lcPurchasePlan.refresh()
        # self.lcPurchaseOrder.refresh()
        # self.lcPurchaseShipped.refresh()
        pass
        
    # def OnPlanedListLeftDclick(self, event):
        # import DlgPurchasePlanEditor
        
        # item = self.lcPurchasePlan.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        # if (item == -1):
            # return
        
        # dlg = DlgPurchasePlanEditor.create(self)
        # dlg.setData(proId=proId, unit=unit, price=price)
        # if (dlg.ShowModal() == wx.ID_OK):
            
        
    # def OnOrderedListLeftDclick(self, event):
        # pass
        
    # def OnShippedListLeftDclick(self, event):
        # pass

    def OnCheckBox_less30Checkbox(self, event):
        self.checkBox_only_warning.SetValue(False)
        self.proList.setOnlyLess30(self.checkBox_less30.GetValue())
        self.proList.refresh()
        event.Skip()

    def ChangeSkuSearch(self, sku):
        self.m_sku = sku
        print sku
        self.refreshProductList()